package com.woniu.car.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.car.common.BeanCopyUtils;
import com.woniu.car.config.MyRedisTemplate;
import com.woniu.car.domain.TBackstageAccount;

import com.woniu.car.exception.AccountException;
import com.woniu.car.mapper.TBackstageAccountMapper;
import com.woniu.car.param.LoginBackstageAccountParam;
import com.woniu.car.param.RegisterBackstageAccountParam;
import com.woniu.car.service.TBackstageAccountService;
import com.woniu.car.util.AccountStatus;
import com.woniu.car.util.IsSuper;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author WWX
 * @since 2021-06-19
 */
@Service
public class TBackstageAccountServiceImpl extends ServiceImpl<TBackstageAccountMapper, TBackstageAccount> implements TBackstageAccountService {
    @Resource
    private MyRedisTemplate myRedisTemplate;
    @Resource
    private TBackstageAccountMapper tBackstageAccountMapper;


    /**
     *@Author 王维鑫
     *@Description 新增后台账号
     *@Param
     *@return
     **/
    @Override
    @Transactional
    public TBackstageAccount insertBackstageAccount(TBackstageAccount tBackstageAccount)  {
        Date date = new Date();
        long time = date.getTime();
        TBackstageAccount tBackstageAccount1 = new TBackstageAccount();
        tBackstageAccount.setBackstageAccount(tBackstageAccount.getBackstageAccount());
        List<TBackstageAccount> tBackstageAccounts = this.queryBackstageAccount(tBackstageAccount1);
        if(tBackstageAccounts.size()!=0){
            throw new AccountException("该账号已经存在");
        }
        tBackstageAccount.setRegisterTime(time);
        tBackstageAccount.setBackstageStatus(AccountStatus.NORMAL);
        tBackstageAccount.setIsSuper(IsSuper.NORMAL);
        int insert = tBackstageAccountMapper.insert(tBackstageAccount);
        if(insert<1){
            throw new AccountException("账号注册失败");
        }
        return tBackstageAccount;
    }
    /**
     *@Author 王维鑫
     *@Description 修改后台账号
     *@Param
     *@return
     **/
    @Override
    @Transactional
    public void updateBackstageAccount(TBackstageAccount tBackstageAccount)  {
        if(tBackstageAccount ==null || tBackstageAccount.equals("")){
            throw new AccountException("修改内容错误");
        }
        boolean b = this.updateById(tBackstageAccount);
        if(b==false){
            throw new AccountException("修改失败");
        }
    }
    /**
     *@Author 王维鑫
     *@Description 查看后台账号列表
     *@Param
     *@return
     **/
    @Override
    @Transactional
    public List<TBackstageAccount> showBackstageAccount() {
        List<TBackstageAccount> list = this.list();
        return list;
    }
    /**
     *@Author 王维鑫
     *@Description 查找后台账号
     *@Param
     *@return
     **/
    @Override
    @Transactional
    public List<TBackstageAccount> queryBackstageAccount(TBackstageAccount tBackstageAccount) {
        QueryWrapper<TBackstageAccount> wrapper = new QueryWrapper<>();
        if(null!=tBackstageAccount.getBackstageAccount()){
            wrapper.eq("backstage_account",tBackstageAccount.getBackstageAccount());
        }
        if(null!=tBackstageAccount.getBackstageId()){
            wrapper.eq("backstage_id",tBackstageAccount.getBackstageId());
        }
        if(null!=tBackstageAccount.getBackstageStatus() ){
            wrapper.eq("backstage_status",tBackstageAccount.getBackstageStatus());
        }
        List<TBackstageAccount> list = this.list(wrapper);
        return list;
    }
    /**
     *@Author 王维鑫
     *@Description 账号注册
     *@Param 
     *@return 
     **/
    @Override
    @Transactional
    public TBackstageAccount registerAccount(RegisterBackstageAccountParam param)  {
        TBackstageAccount tBackstageAccount = new TBackstageAccount();
        tBackstageAccount.setBackstageAccount(param.getBackstageAccount());
        List<TBackstageAccount> tBackstageAccounts = this.queryBackstageAccount(tBackstageAccount);
        if(tBackstageAccounts.size()==0){
            BeanCopyUtils.copyProperties(param,tBackstageAccount);
            TBackstageAccount tBackstageAccount1 = this.insertBackstageAccount(tBackstageAccount);
            List<TBackstageAccount> list = this.queryBackstageAccount(tBackstageAccount1);
            HashOperations hash = myRedisTemplate.opsForHash();
            hash.put("backstage_account",list.get(0).getBackstageAccount(),list.get(0));
            return list.get(0);
        }else {
            throw new AccountException("该账号已存在，请更换");
        }

    }
    /**
     *@Author 王维鑫
     *@Description 账号登录
     *@Param
     *@return
     **/
    @Override
    @Transactional
    public TBackstageAccount login(LoginBackstageAccountParam param)  {
//        HashOperations hash = myRedisTemplate.opsForHash();
//        TBackstageAccount backstageAccount = (TBackstageAccount) hash.get("backstage_account", param.getBackstageAccount());
        TBackstageAccount tBackstageAccount2 = new TBackstageAccount();
        tBackstageAccount2.setBackstageAccount(param.getBackstageAccount());
        List<TBackstageAccount> tBackstageAccounts1 = this.queryBackstageAccount(tBackstageAccount2);
        if(tBackstageAccounts1.size()==0){
            throw new AccountException("账号不存在");
        }else {
            String backstagePwd = tBackstageAccounts1.get(0).getBackstagePwd();
            if(!backstagePwd.equals(param.getBackstagePwd())){
                throw new AccountException("密码错误");
            }else {
                return tBackstageAccounts1.get(0);
            }
        }
//        if(backstageAccount==null){
//            TBackstageAccount tBackstageAccount = new TBackstageAccount();
//            BeanCopyUtils.copyProperties(param,tBackstageAccount);
//            List<TBackstageAccount> tBackstageAccounts = this.queryBackstageAccount(tBackstageAccount);
//            System.out.println("传入："+backstageAccount.getBackstagePwd());
//            TBackstageAccount tBackstageAccount1 = tBackstageAccounts.get(0);
//            String str = tBackstageAccount1.getBackstagePwd();
//            if(tBackstageAccounts.get(0)!=null){
//                return tBackstageAccounts.get(0);
//            }else {
//                throw new AccountException("账号或密码错误");
//            }
//        }else if(param.getBackstagePwd().equals()){
//            throw new AccountException("密码错误");
//        }else {
//            return backstageAccount;
//        }
    }

    @Override
    @Transactional
    public TBackstageAccount insertSuperAccount(TBackstageAccount tBackstageAccount) {
        Date date = new Date();
        long time = date.getTime();
        tBackstageAccount.setRegisterTime(time);
        tBackstageAccount.setBackstageStatus(AccountStatus.NORMAL);
        tBackstageAccount.setIsSuper(IsSuper.SUPER);
        int insert = tBackstageAccountMapper.insert(tBackstageAccount);
        if(insert<1){
            throw new AccountException("账号注册失败");
        }
        return tBackstageAccount;
    }


}
